<?php
namespace app\common\service\GoodsService;

use app\model\BbcModel\ChannelBusiness;
use app\model\BbcModel\GoodsComment;
use app\model\BbcModel\GoodsCommentTag;
use app\model\BbcModel\Store;
use app\Normal;
use think\facade\Db;
use think\facade\Request;

class GoodsCommentService {

    //每页行数
    protected $row = 10;

    /**
     * 用户反馈列表
     * @param array $param
     * @return void
     * @author huj
     */
    public function index(array $param = []) {
        $model = GoodsComment::instance();
        if (Request::has('order_sn', 'param', true)) {
            $model = $model->where('sn', $param['order_sn']);
        };
        if (Request::has('goods_name', 'param', true)) {
            $model = $model->whereLike('goods_name', "%" . $param['goods_name'] . "%");
        };
        if (Request::has('barcode', 'param', true)) {
            $model = $model->where('barcode', $param['barcode']);
        };
        if (Request::has('skucode', 'param', true)) {
            $model = $model->where('skucode', $param['skucode']);
        };
        if (Request::has('nickname', 'param', true)) {
            $model = $model->whereLike('nickname', "%" . $param['nickname'] . "%");
        };
        if (Request::has('mobile', 'param', true)) {
            $model = $model->where('mobile', $param['mobile']);
        };
        if (Request::has('check_status', 'param', true) && in_array($param['check_status'], ['1', '2', '3'])) {
            $model = $model->where('check_status', $param['check_status']);
        };
        if (Request::has('mn_sid', 'param', true)) {
            $model = $model->where('mn_sid', $param['mn_sid']);
        };
        if (Request::has('star', 'param', true) && in_array($param['star'], ['1', '2', '3', '4', '5'])) {
            $model = $model->where('star', $param['star']);
        };
        if (Request::has('start_time', 'param', true)) {
            $model = $model->where('cd', '>=', $param['start_time']);
        };
        if (Request::has('end_time', 'param', true)) {
            $model = $model->where('cd', '<=', $param['end_time']);
        };
        if (Request::has('limit', 'param', true)) {
            $this->row = Request::param('limit');
        }
        $fields = [
            'id', 'mid', 'mn_sid', 'cb_id', 'sn as order_sn', 'goods_name', 'skucode', 'barcode',
            'nickname', 'mobile', 'content', 'check_status', 'imgs', 'cd', 'star',
        ];
        $result = $model->field($fields)->order(['cd' => 'desc', 'id' => 'desc'])->paginate($this->row, false, ['query' => Request::param()]);
        if (!$result->isEmpty()) {
            $result->append(['check_status_text', 'cd_text']);
            $mnids     = $result->column('mn_sid');
            $cbids     = $result->column('cb_id');
            $cbInfo    = ChannelBusiness::whereIn('id', $cbids)->where('status', 1)->where('check_status', 1)->column('name', 'id');
            $storeInfo = Store::whereIn('id', $mnids)->where('status', 1)->where('check_status', 1)->where('is_pay', 1)->column('store_name', 'id');
            foreach ($result->items() as $key => $value) {
                $value->cb_name    = ($cbInfo[$value->cb_id]) ?? '';
                $value->store_name = ($storeInfo[$value->mn_sid]) ?? '';
                $value->imgs       = $value->imgs ? json_decode($value->imgs) : $value->imgs;
            }
        }
        return Normal::returnCode(Normal::SUC, "请求成功", ['total' => $result->total(), 'list' => $result->items()]);
    }

    /**
     * 审核商品评价
     * @param array $param
     * @return void
     * @author huj
     */
    public function handleCheckStatus(array $param = []) {
        $validate = \think\facade\Validate::rule([
            'id'           => ['require', 'regex' => '/^[1-9]\d*$/'],
            'check_status' => ['require', 'in' => '2,3'],
        ])->message([
            'id.require'           => '评价id不能为空',
            'id.regex'             => '评价id值格式错误',
            'check_status.require' => '审核状态不能为空',
            'check_status.in'      => '审核状态值错误',
        ]);
        if (!$validate->check($param)) {
            return Normal::returnCode(Normal::FAIL, $validate->getError());
        }
        Db::startTrans();
        try {
            $comment = GoodsComment::where('id', $param['id'])->field(['id', 'check_status'])->find();
            if (!$comment) {
                throw new \Exception("评价不存在");
            }
            if ($comment->check_status != GoodsComment::CHECKING) {
                throw new \Exception("该条评价已审核");
            }
            $comment->check_status = (int) $param['check_status'];
            $result                = $comment->save();
            if (!$result) {
                throw new \Exception("评价审核失败");
            }
            GoodsCommentTag::whereIn('comment_id', $param['id'])->update(['check_status' => $param['check_status'], 'md' => time()]);
            // 提交事务
            Db::commit();
        } catch (\Throwable $th) {
            // 回滚事务
            Db::rollback();
            return Normal::returnCode(Normal::FAIL, "审核失败【{$th->getMessage()}】");
        }
        return Normal::returnCode(Normal::SUC, "审核成功");
    }

}